//
//  homeViewController.m
//  One Construction
//
//  Created by apple on 2019/9/3.
//  Copyright © 2019 apple. All rights reserved.
//

#import "homeViewController.h"
#import "ViewController.h"
#import "homeModel.h"
#import "detailHomeViewController.h"
#import "homeTableViewCell.h"
#import "secondViewController.h"
#import "tabViewController.h"
@interface homeViewController ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UIImageView *headImgView;
@property(nonatomic,strong)NSMutableArray *imageArr;

@property(nonatomic,strong)UIView *headView;
@property(nonatomic,strong)UITableView *TableView;
@property(nonatomic,strong)NSMutableArray *mainDataArr;
@property(nonatomic,strong) SDCycleScrollView *sd2 ;
@property(nonatomic,strong)UICollectionView *collecView;
@property(nonatomic,strong)NSArray *collecViewArr;

@end

@implementation homeViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   self.title  = @"首页";
//    self.navigationItem.title = @"首页";
    _imageArr = [[NSMutableArray alloc]init];
   // [self getData];
    _collecViewArr = @[@"collectinview",@"collectinview1",@"collectinview2"];
    [self.view addSubview:self.headView];
    // AppNavitionHright/2-864+44+30
    myLable *navlable = [[myLable alloc]initWithFrame:CGRectMake(0,AppNavitionHright/2+NavBarSafetyZone, ScreenWidth, 17)];
    navlable.textAlignment = NSTextAlignmentCenter;
    [self.headView addSubview:navlable];
    self.headView.userInteractionEnabled= YES;
    navlable.userInteractionEnabled = YES;
//    navlable.backgroundColor = [UIColor redColor];
    [navlable setLabletextColor:[UIColor whiteColor] Text:@"一级建造师" TextFont:18];
    
    [self getTheMainData];
    [self creatUI];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)creatUI{
//在加载的时候开始调用z自动的滚动试图
    self.sd2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(10,AppNavitionHright, ScreenWidth-20, 170) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    if (self.imageArr.count == 0) {
        [self getData];
    }
    //自定义的页面控件颜色
   self.sd2.currentPageDotColor =  [UIColor colorWithRed:26/255.0 green:74/255.0 blue:199/255.0 alpha:1.0];
    //    [sd2 disableScrollGesture];
    self.sd2.pageControlAliment  = SDCycleScrollViewPageContolAlimentCenter;
    //其他分页控件小图标颜色
    self.sd2.pageDotColor = [UIColor grayColor];
    //自动滚动时间间隔,默认2s
    self.sd2.autoScrollTimeInterval = 3;
    //是否自动滚动, 默认YES
    self.sd2.autoScroll = YES;
    //轮播图片的ContentMode, 默认为UIViewContentModeScaleToFill
    //_scrollView.bannerImageViewContentMode = UIViewContentModeCenter;
    //是否无限循环,默认YES: 滚动到第四张图就不再滚动了
    self.sd2.infiniteLoop = YES;
    [self.view addSubview:self.sd2];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        NSArray *arr= [NSArray arrayWithArray:self.imageArr];
//        sd2.imageURLStringsGroup = arr;
//    });
//
   
    
    
    
    UIView *LatestCourseView= [[UIView alloc] initWithFrame:CGRectMake(10, AppNavitionHright+170+12, ScreenWidth, 25)];
    [self creatLatestView:LatestCourseView lableText:@"最新课程"];

//    for (int i = 0; i<2; i++) {
//        UIImageView *courceImgView =[[UIImageView alloc]init];
//        courceImgView.backgroundColor = [UIColor redColor];
//        [self.view addSubview:courceImgView];
//       CGFloat wid = ScreenWidth-340;
//
//
//            [courceImgView mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.mas_offset(10+i*wid+i*160);
//     make.top.mas_equalTo(LatestCourseView.mas_bottom).mas_offset(9);
//                make.width.mas_equalTo(160);
//                 make.height.mas_equalTo(110);
//            }];
//             }
     [self.view addSubview:self.collecView];
    [self.collecView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
  make.top.mas_equalTo(LatestCourseView.mas_bottom).mas_offset(9);
    make.width.mas_equalTo(ScreenWidth);
    make.height.mas_equalTo(110);
    }];
    /// 设置此属性为yes 不满一屏幕 也能滚动
    self.collecView.alwaysBounceHorizontal = YES;
    self.collecView.showsHorizontalScrollIndicator = NO;
    
    
    
    
    UIView *importantView= [[UIView alloc] initWithFrame:CGRectMake(10, LatestCourseView.frame.origin.y+LatestCourseView.frame.size.height+19+110, ScreenWidth, 25)];
    [self creatLatestView:importantView lableText:@"重要资讯"];
    
    [self.view addSubview:self.TableView];
    [self.TableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(0);
          make.left.mas_offset(0);
    make.top.mas_equalTo(importantView.mas_bottom).mas_offset(8);
        make.bottom.mas_offset(-TabbarSafetyZone);
    }];
    MJRefreshGifHeader *head = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getTheMainData)];
    self.TableView.mj_header = head;
    [ self.TableView.mj_header beginRefreshing];
    
    
    
}

- (void)creatLatestView:(UIView *)viewc lableText:(NSString *)labletext{
    [self.view addSubview:viewc];
    viewc.backgroundColor = [UIColor whiteColor];
    
    myLable *lable = [[myLable alloc]initWithFrame:CGRectMake(10, 4, 80, 17)];

    [viewc addSubview:lable];
    [lable setLabletextColor:[UIColor blackColor] Text:labletext TextFont:18];

    UIView *blueview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 3, 25)];
    blueview.backgroundColor = [UIColor colorWithRed:27/255.0 green:82/255.0 blue:225/255.0 alpha:1.0];
    [viewc addSubview:blueview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mainDataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *homeTableCell = @"homeTableCell";
    homeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:homeTableCell];
    homeModel *model = self.mainDataArr[indexPath.row];
    cell.readerImgView.image = [UIImage imageNamed:@"read"];
     cell.dataImgView.image = [UIImage imageNamed:@"data"];

    [cell.headImgView sd_setImageWithURL:[NSURL URLWithString:model.pic]];
    cell.titleLable.text = model.title;
    NSString *dataString = [NSString stringWithFormat:@"%d",model.yd_rs];
    cell.numReaderLable.text = [NSString stringWithFormat:@"已阅读人数%@",dataString];
    cell.dataLable.text = model.rq;
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    detailHomeViewController *de = [[detailHomeViewController alloc]init];

    
    homeModel *model = _mainDataArr[indexPath.row];
    de.urlString = model.url;
    
    
    
    [self.navigationController pushViewController:de animated:YES];
//    ViewController *vc = [[ViewController alloc]init];
//    [self presentViewController:vc animated:YES completion:^{
//
//    }];
//    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)getData{
   
    NSDictionary *dict = @{
                            @"lb":@"一级建造师"
                            };
   
    [CZNetWork post:@"https://i.zhongjianedu.com/ios.php/104/sy/sygg" params:dict success:^(id success) {
      NSArray *arry = success[@"sygg_list"];
        [self.imageArr removeAllObjects];
        
        for (int i = 0;i<arry.count;i++) {

            [self.imageArr addObject:arry[i][@"pic"]];

        }
//        NSMutableArray *arr = [[NSMutableArray alloc]init];
    
        NSArray *arr= [NSArray arrayWithArray:self.imageArr];
        self.sd2.imageURLStringsGroup = arr;

        
       
    } failure:^(NSError *error) {
        NSLog(@"error");
    }];
    
    
}
//获取首页重要资讯的代码
- (void)getTheMainData{
    
    NSDictionary *dict = @{
                           @"lb":@"一级建造师"
                           };
    
    
    __weak __typeof__(self)weakSelf = self;
    [CZNetWork post:@"https://i.zhongjianedu.com/ios.php/104/sy/sygg" params:dict success:^(id success) {
        NSArray *arry = success[@"news_list"];
        [self.mainDataArr removeAllObjects];
        
        
        
        for (int i = 0;i<arry.count;i++) {
            homeModel *model = [homeModel mj_objectWithKeyValues:arry[i]];
            [self.mainDataArr addObject:model];
            
        }
     
        [weakSelf. TableView reloadData];
        [weakSelf.TableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"error");
    }];
    
    
    
    
}
- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, -NavBarSafetyZone, ScreenWidth, 209)];
        _headView.backgroundColor = [UIColor colorWithRed:26/255.0 green:74/255.0 blue:199/255.0 alpha:1.0];
        
//        CAGradientLayer *gradient = [CAGradientLayer layer];
//        //设置开始和结束位置(设置渐变的方向)
//        gradient.startPoint = CGPointMake(0, 1);
//        gradient.endPoint = CGPointMake(0, 1);
//        gradient.frame =CGRectMake(0,0,ScreenWidth,100);
//
//        gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:26/255.0 green:74/255.0 blue:199/255.0 alpha:1.0].CGColor,(id)[UIColor whiteColor].CGColor,nil];
//        [_headView.layer insertSublayer:gradient atIndex:0];
//
        _headView.userInteractionEnabled = YES;
       
        
        
        
        
}
    
    return _headView;
}
#pragma mark -- collectionview的代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _collecViewArr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *collceCellID = @"collectioncellid";
    
    [_collecView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:collceCellID];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collceCellID forIndexPath:indexPath];
    //防止复用
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
        
    }
//    cell.backgroundColor = [UIColor redColor];
    
    UIImageView *picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160, 110)];
    picture.image = [UIImage imageNamed:_collecViewArr[indexPath.row]];
    [cell.contentView addSubview:picture];
    
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(160, 110);
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    self.navigationController.tabBarController.hidesBottomBarWhenPushed = NO;
       self.navigationController.tabBarController.selectedIndex=1;
    [self.navigationController popViewControllerAnimated:YES];
   
    
    
}




- (UITableView *)TableView{
    if (!_TableView) {
        _TableView = [[UITableView alloc]init];
        _TableView.delegate = self;
        _TableView.dataSource =self;
        
        [_TableView registerClass:[homeTableViewCell class] forCellReuseIdentifier:@"homeTableCell"];
      
    }
    return _TableView;
}
- (NSMutableArray *)mainDataArr{
    
    if (!_mainDataArr) {
        _mainDataArr = [[NSMutableArray alloc]init];
    }
    return _mainDataArr;
    
}
- (UICollectionView *)collecView{
    if (!_collecView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//         layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collecView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collecView.delegate = self;
        _collecView.dataSource =self;
        _collecView.backgroundColor = [UIColor whiteColor];
        
    }
    return _collecView;
    
}
@end
